//
//  ComputingPowerView.swift
//  SCA
//
//  Created by tjun on 2025/9/17.
//

import UIKit
import SnapKit

class ComputingPowerView: UIView {
    
    private let computingPowerValueLabel = UILabel(font: UIFontSemiboldMake(20), textColor: .white)
    private let vitalityValueLabel = UILabel(font: UIFontSemiboldMake(20), textColor: .white)
    private let arrowImageView = UIImageView(image: UIImage(named: "ev_arrow"))
    var onInfoButtonTapped: (() -> Void)?

    var vitalityValue: Float = 0 {
        didSet {
           vitalityValueLabel.text = String(vitalityValue).formattedDecimal(decimals: 2)
            arrowImageView.isHidden = vitalityValue <= 0
        }
    }
    
    var computingPowerValue: Float = 0 {
        didSet {
            computingPowerValueLabel.text = String(computingPowerValue).formattedDecimal(decimals: 2)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let textLabel = UILabel(text: "算力".localizedString, font: UIFontSemiboldMake(14), textColor: .white)
        textLabel.addTo(self)
        textLabel.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.top.equalTo(14)
        }
        
        let unitLabel = UILabel(text: "G", font: UIFontSemiboldMake(14), textColor: UIColorMake(0x0062FE))
        unitLabel.addTo(self)
        unitLabel.snp.makeConstraints { make in
            make.right.equalTo(-14)
            make.centerY.equalTo(textLabel)
        }
        
        computingPowerValueLabel.addTo(self)
        computingPowerValueLabel.snp.makeConstraints { make in
            make.right.equalTo(-45)
            make.centerY.equalTo(textLabel)
        }
        
        let vitalityLabel = UILabel(text: "活力值".localizedString, font: UIFontSemiboldMake(14), textColor: .white)
        vitalityLabel.addTo(self)
        vitalityLabel.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.top.equalTo(textLabel.snp.bottom).offset(15)
            make.bottom.equalTo(-14)
        }
        
        let infoButton = UIButton(type: .system).addTo(self)
        infoButton.addTarget(self, action: #selector(infoButtonTapped), for: .touchUpInside)
        infoButton.setImage(UIImage(named: "more-info-icon"), for: .normal)
        infoButton.snp.makeConstraints { make in
            make.width.height.equalTo(20)
            make.left.equalTo(vitalityLabel.snp.right).offset(8)
            make.centerY.equalTo(vitalityLabel)
        }
        
        let vitalityUnitLabel = UILabel(text: "EV", font: UIFontSemiboldMake(14), textColor: UIColorMake(0x0062FE))
        vitalityUnitLabel.addTo(self)
        vitalityUnitLabel.snp.makeConstraints { make in
            make.right.equalTo(-14)
            make.centerY.equalTo(vitalityLabel)
        }
        
        vitalityValueLabel.addTo(self)
        vitalityValueLabel.snp.makeConstraints { make in
            make.right.equalTo(computingPowerValueLabel)
            make.centerY.equalTo(vitalityLabel)
        }
        
        arrowImageView.addTo(self)
        arrowImageView.isHidden = true
        arrowImageView.snp.makeConstraints { make in
            make.right.equalTo(vitalityValueLabel.snp.left).offset(-6)
            make.centerY.equalTo(vitalityValueLabel)
        }
    }
    
    @objc func infoButtonTapped() {
        onInfoButtonTapped?()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


class BuyComputingPowerAmountView: UIView {
    
    private let coinView = UIView()
    let coinIconImageView = UIImageView()

    let coinNameLabel = UILabel(font: UIFontSemiboldMake(20), textColor: .white)
    let coinAmountLabel = UILabel(font: UIFontSemiboldMake(20), textColor: .white)
    let totalAmountLabel = UILabel(text: "总金额".localizedString, font: UIFontSemiboldMake(20), textColor: .white)
    let totalAmountValueLabel = UILabel(font: UIFontSemiboldMake(20), textColor: .white)

    private var coinViewHeightConstraint: Constraint!
    
    var isShowCoin: Bool = false {
        didSet {
            if isShowCoin {
                coinView.isHidden = false
                UIView.animate(withDuration: 0.25) {
                    self.coinViewHeightConstraint.update(offset: 56)
                    self.layoutIfNeeded()
                }
            } else {
                coinView.isHidden = true
                UIView.animate(withDuration: 0.25) {
                    self.coinViewHeightConstraint.update(offset: 0)
                    self.layoutIfNeeded()
                }
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        coinView.addTo(self)
        coinView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            coinViewHeightConstraint = make.height.equalTo(0).constraint
        }
        
        coinIconImageView.addTo(coinView)
        coinIconImageView.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(32)
        }
        
        coinNameLabel.addTo(coinView)
        coinNameLabel.snp.makeConstraints { make in
            make.left.equalTo(coinIconImageView.snp.right).offset(12)
            make.centerY.equalToSuperview()
        }
        
        coinAmountLabel.addTo(coinView)
        coinAmountLabel.snp.makeConstraints { make in
            make.right.equalTo(-12)
            make.centerY.equalToSuperview()
        }
        
        let lineView = UIView().addTo(coinView)
        lineView.backgroundColor = UIColor.white.withAlphaComponent(0.3)
        lineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(12)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
        let amountView = UIView()
        amountView.addTo(self)
        amountView.snp.makeConstraints { make in
            make.top.equalTo(coinView.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(52)
            make.bottom.equalToSuperview()
        }
        
        totalAmountLabel.addTo(amountView)
        totalAmountLabel.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.centerY.equalToSuperview()
        }
        
        totalAmountValueLabel.addTo(amountView)
        totalAmountValueLabel.snp.makeConstraints { make in
            make.right.equalTo(-12)
            make.centerY.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}






